package mnnu.ghbe.service;

import mnnu.ghbe.entity.Ranking;
import mnnu.ghbe.entity.User;
import mnnu.ghbe.repository.RankingRepository;
import mnnu.ghbe.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RankingService {
    
    @Autowired
    private RankingRepository rankingRepository;
    
    @Autowired
    private UserRepository userRepository;
    
    public List<Ranking> getAllRankings() {
        return rankingRepository.findAllByOrderByScoreDesc();
    }
    
    public List<RankingDTO> getTop15Rankings() {
        List<Ranking> rankings = rankingRepository.findAllByOrderByScoreDesc();
        // 只取前15个
        if (rankings.size() > 15) {
            rankings = rankings.subList(0, 15);
        }
        
        // 将Ranking转换为包含用户信息的DTO
        return rankings.stream().map(ranking -> {
            User user = userRepository.findById(ranking.getUserId()).orElse(null);
            return new RankingDTO(ranking, user);
        }).collect(Collectors.toList());
    }
    
    public RankingResult getTop15RankingsWithUserRanking(Long userId) {
        List<Ranking> rankings = rankingRepository.findAllByOrderByScoreDesc();
        
        // 查找指定用户的排名
        int userRank = -1;
        Ranking userRanking = null;
        for (int i = 0; i < rankings.size(); i++) {
            if (rankings.get(i).getUserId().equals(userId)) {
                userRank = i + 1; // 排名从1开始
                userRanking = rankings.get(i);
                break;
            }
        }
        
        // 只取前15个
        if (rankings.size() > 15) {
            rankings = rankings.subList(0, 15);
        }
        
        // 将Ranking转换为包含用户信息的DTO
        List<RankingDTO> top15 = rankings.stream().map(ranking -> {
            User user = userRepository.findById(ranking.getUserId()).orElse(null);
            return new RankingDTO(ranking, user);
        }).collect(Collectors.toList());
        
        // 获取用户信息
        RankingDTO userRankingDTO = null;
        if (userRanking != null) {
            User user = userRepository.findById(userId).orElse(null);
            userRankingDTO = new RankingDTO(userRanking, user);
        }
        
        return new RankingResult(top15, userRankingDTO, userRank);
    }
    
    // 创建一个RankingDTO类来包含排行榜信息和用户信息
    public static class RankingDTO {
        private Long userId;
        private Integer score;
        private String nickname;
        private String avatar;
        
        public RankingDTO(Ranking ranking, User user) {
            this.userId = ranking.getUserId();
            this.score = ranking.getScore();
            this.nickname = user != null ? user.getNickname() : "未知用户";
            this.avatar = user != null ? user.getAvatar() : "";
        }
        
        // Getters and Setters
        public Long getUserId() {
            return userId;
        }
        
        public void setUserId(Long userId) {
            this.userId = userId;
        }
        
        public Integer getScore() {
            return score;
        }
        
        public void setScore(Integer score) {
            this.score = score;
        }
        
        public String getNickname() {
            return nickname;
        }
        
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
        
        public String getAvatar() {
            return avatar;
        }
        
        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }
    }
    
    // 创建一个RankingResult类来封装排行榜结果和用户自己的排名
    public static class RankingResult {
        private List<RankingDTO> top15;
        private RankingDTO userRanking;
        private Integer userRank;
        
        public RankingResult(List<RankingDTO> top15, RankingDTO userRanking, Integer userRank) {
            this.top15 = top15;
            this.userRanking = userRanking;
            this.userRank = userRank;
        }
        
        // Getters and Setters
        public List<RankingDTO> getTop15() {
            return top15;
        }
        
        public void setTop15(List<RankingDTO> top15) {
            this.top15 = top15;
        }
        
        public RankingDTO getUserRanking() {
            return userRanking;
        }
        
        public void setUserRanking(RankingDTO userRanking) {
            this.userRanking = userRanking;
        }
        
        public Integer getUserRank() {
            return userRank;
        }
        
        public void setUserRank(Integer userRank) {
            this.userRank = userRank;
        }
    }
}